import { defaultAvatarUrl } from "../../config/site"
import { nickNameGet, nickNameSet, avatarGet, avatarSet } from "../../storage/userInfo"

Component({

  properties: {

  },

  data: {
    // canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    // canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    avatarUrl: defaultAvatarUrl,
    nickName: '',
  },

  lifetimes: {
    attached: function () {
      this.loadUserInfoFromCache()
    },
  },

  methods: {
    loadUserInfoFromCache() {
      const nickName = nickNameGet();
      if (nickName) {
        this.setData({ nickName });
      }
      const avatarUrl = avatarGet();
      if (avatarUrl) {
        this.setData({ avatarUrl });
      }
    },

    onChooseAvatar(e) {
      const { avatarUrl } = e.detail;
      if (!avatarUrl) return;
      avatarSet(avatarUrl);
      this.setData({ avatarUrl });
    },

    onInputChange(e) {
      const nickName = e.detail.value
      if (!nickName) return;
      nickNameSet(nickName)
      this.setData({ nickName })
    },
  }
})